package cn.java.sy;

import java.security.NoSuchAlgorithmException;

public class MD5Utils {
	public static String digest(byte[] source) {

		java.security.MessageDigest md;

		try {
			md = java.security.MessageDigest.getInstance("MD5");
			md.update(source);
			byte tmp[] = md.digest();

			return CodeByte.encode(tmp);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String digest(String source) {
		if (source == null) {
			return null;
		}
		return digest(source.getBytes());
	}
	
	

	public static String Bit32(String source) {
	    return MD5Utils.digest(source);
	}

	public static String Bit16(String source)  {
	    return Bit32(source).substring(8, 24);
	}

	public static void main(String[] a) {

		for (int i = 0; i < 3; i++) {
			String s = "Ab!@#456" + i;

			String d = MD5Utils.digest(s);
			
			System.out.print(d);
			System.out.println(" "+d.length());
			
			d = MD5Utils.Bit32(s);
			System.out.print(d);
			System.out.println(" "+d.length());
			
			d = MD5Utils.Bit16(s);
			System.out.print(d);
			System.out.println(" "+d.length());
			
			
			
		}

		String s = "1";
		String d = MD5Utils.digest(s);
		
		System.out.print(d);
		System.out.println(" "+d.length());
		
		d = MD5Utils.Bit32(s);
		System.out.print(d);
		System.out.println(" "+d.length());
		
		d = MD5Utils.Bit16(s);
		System.out.print(d);
		System.out.println(" "+d.length());

	}
}
